package com.ruoyi.mall.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.mall.domain.vo.MallOrderVo;
import com.ruoyi.mall.service.IWechatService;
import com.ruoyi.mall.utils.HttpClientUtil;
import com.ruoyi.mall.utils.StringUtils;
import com.ruoyi.mall.wechat.WechatPayDto;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/wechat")
public class WeChatController
{

    @Autowired
    IWechatService wechatService;

    public static String WXPUBLIC_APP_ID ="wx89289c3a3da01e68";
    // 小程序开发者秘钥
    private static final String WXPUBLIC_SECRET = "8dfe295275dbe4c3a0ee8de5d9840814";

    private static final Logger logger = LoggerFactory.getLogger(WeChatController.class);

    /**
     * 根据code获取openId
     * @param json
     * @return
     */
    @RequestMapping("/getOpenId")
    public R<JSONObject> getOpenId(@RequestBody JSONObject json)
    {
        String code = json.get("code").toString();
        logger.info("获取到微信" + code);
        //获取access_token
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token" + "?appid=" + WXPUBLIC_APP_ID + "&secret=" + WXPUBLIC_SECRET + "&code=" + code + "&grant_type=authorization_code";
        String tokenResult = HttpClientUtil.doGet(url);
        logger.info("请求获取access_token:" + tokenResult);
        //返回结果的json对象
        JSONObject resultObject = JSONUtil.parseObj(tokenResult);//请求获取userInfo
        String openId = resultObject.getStr("openid");
        if (!StringUtils.isNullOrEmpty(openId))
        {
            return R.ok(resultObject);
        }
        return R.fail();
    }

    /**
     * 微信预支付
     * @param
     * @return 信息提示
     */
    @PostMapping("/orderPay")
    public R<WechatPayDto> orderPay(@RequestBody MallOrderVo order) {
        return null;
//        WechatPayReqParam reqParam = new WechatPayReqParam();
//        WechatPayDto wechatPayDto = wechatService.jsPay(reqParam);
//        if (ObjectUtil.isNotEmpty(wechatPayDto)) {
//            return R.ok(wechatPayDto);
//        } else {
//            return R.fail("请求错误");
//        }
    }

//    /**
//     * 支付成功回调
//     * 这个是给微信访问的 注意的是 不要拦截这个接口 也不要进行鉴权，否则回调调用不到
//     */
//    @PostMapping("/payNotify")
//    public ResponseEntity.BodyBuilder payNotify(HttpServletRequest request, HttpServletResponse response){
//        System.out.println("微信支付成功回调进来了");
//        return wechatService.payNotify(request, response);
//
//    }
//
//    /**
//     * 申请退款
//     */
//    @PostMapping("/vxPayRefund")
//    public R<Void> payRefund(@RequestBody @Validated WechatPayRefundReqParam param){
//        String s = wechatService.payRefund(param);
//        if (ObjectUtil.isNotEmpty(s)){
//            return R.ok("退款申请已发起！");
//        } else {
//            return R.fail("退款失败，请稍后再试！");
//        }
//    }
//
//    /**
//     * 退款回调
//     * 这个是给微信访问的 注意的是 不要拦截这个接口 也不要进行鉴权，否则回调调用不到
//     */
//    @PostMapping("/refundNotify")
//    public String refundNotify(HttpServletRequest request, HttpServletResponse response){
//        return wechatService.refundNotify(request, response);
//    }

}
